package com.bawei.a2010douyin.widget;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 贝塞尔曲线
 */
public class AoLiGeiView extends RelativeLayout {

    //图片集合
    private List<Integer> mList = new ArrayList<>();
    //添加图片
    public void addImages(Integer... src){//Integer... 多个值
        mList.addAll(Arrays.asList(src));//将Integer...转成集合--》添加到集合中
    }
    //动画:创建imageview--》添加到容器中--》动画--》动画结束移除
    public void addFavor(){
        //0：获得当前容器的宽度和高度
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        //1:创建imageview
        ImageView imageView = new ImageView(getContext());
        LayoutParams layoutParams = new LayoutParams(50, 50);
        imageView.setLayoutParams(layoutParams);
        int index = (int)(Math.random()*mList.size());//随机数 {0,集合长度-1}
        imageView.setImageResource(mList.get(index));
        addView(imageView);
        //2:动画：估值器
        PointF pointF0 = new PointF(width / 2, height);//起点：
        PointF pointF1 = new PointF((float) (Math.random()*width), height*2/3);//中间点1
        PointF pointF2 = new PointF((float) (Math.random()*width), height/3);//中间点2
        PointF pointF3 = new PointF(width / 2, 0);//终点
        ValueAnimator valueAnimator = ValueAnimator.ofObject(new MyEvaluator(pointF1,pointF2),pointF0 ,pointF3 );
        valueAnimator.setDuration(3000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                PointF pointF = (PointF) animation.getAnimatedValue();
                imageView.setX(pointF.x);
                imageView.setY(pointF.y);
                //修改透明度
                float fraction = animation.getAnimatedFraction();//0-1
                imageView.setAlpha( 1 -fraction );
            }
        });
        valueAnimator.start();
        //3:动画结束
        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                removeView(imageView);
            }
        });

    }
    //TODO 1：估值器
    class MyEvaluator implements TypeEvaluator<PointF> {
        private PointF point1 ;
        private PointF point2 ;

        public MyEvaluator(PointF point1, PointF point2) {
            this.point1 = point1;
            this.point2 = point2;
        }

        /**
         * @param t 0-1
         * @param point0 起始点
         * @param point3 结束点
         * @return 得到最终变化的点
         */
        @Override
        public PointF evaluate(float t, PointF point0, PointF point3) {
            PointF pointF = new PointF();
            pointF.x = point0.x*(1-t)*(1-t)*(1-t)
                    +3*point1.x*t*(1-t)*(1-t)
                    +3*point2.x*t*t*(1-t)
                    +point3.x*t*t*t;
            pointF.y = point0.y*(1-t)*(1-t)*(1-t)
                    +3*point1.y*t*(1-t)*(1-t)
                    +3*point2.y*t*t*(1-t)
                    +point3.y*t*t*t;

            return pointF;
        }
    }






    public AoLiGeiView(Context context) {
        super(context);
    }

    public AoLiGeiView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AoLiGeiView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public AoLiGeiView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
}
